from tools.express.register import fun_dict,log
import sys

# @tip 翻译并执行中文伪代码(py . exec in目录下伪代码文件名)
class Main:

    def __init__(self):
        # 获取伪代码内容
        try:
            path = sys.argv[2]
            with open(path, 'r', encoding='utf-8') as fp:
                self.content = fp.readlines()
        except Exception as e:
            print(e)
            print('伪代码文件路径错误！')
            return
        self.exec_zh_code()

    # 翻译并执行中文伪代码
    def exec_zh_code(self):
        # 原始数据，流的开始
        data = None
        for line in self.content:
            # 去除首尾多余的空格
            line = line.strip()
            # 如果当前行没有数据或是以 - 开头 则跳过
            if (not line) or line.startswith('-'):
                continue
            # 打印日志
            if log['open']:
                print(line)
            # 字典中取出对应处理函数，参数与功能名用空格分隔，切分中带参数和不带参数的函数
            space_index = line.find(' ')
            if space_index == -1:
                data = fun_dict[line](data)
            else:
                data = fun_dict[line[:space_index]](data, line[space_index + 1:].lstrip())


